<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '../api'

const router = useRouter()
const username = ref('')
const password = ref('')
const errorMessage = ref('')
const loading = ref(false)

const handleLogin = async () => {
  if (!username.value || !password.value) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  loading.value = true
  errorMessage.value = ''
  try {
    const response = await userApi.login({
      username: username.value,
      password: password.value
    })
    localStorage.setItem('user-token', response.data.token)
    router.push('/todo')
  } catch (err) {
    errorMessage.value = err.response?.data?.message || '登录失败，请检查用户名和密码'
    console.error('登录失败:', err)
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h1>登录</h1>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <input
            v-model="username"
            type="text"
            placeholder="用户名"
            required
          >
        </div>
        <div class="form-group">
          <input
            v-model="password"
            type="password"
            placeholder="密码"
            required
          >
        </div>
        <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
        <button type="submit" class="login-button">登录</button>
      </form>
      <p class="register-link">
        还没有账号？
        <a @click="goToRegister">立即注册</a>
      </p>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #ffffff;
}

.login-box {
  width: 100%;
  max-width: 600px;
  padding: 3rem;
  background: rgba(255, 255, 255, 1);
  backdrop-filter: blur(5000px);
  border-radius: 14px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
}

h1 {
  color: #1d1d1f;
  text-align: center;
  margin-bottom: 3rem;
  font-weight: 500;
  font-size: 2rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 0 2rem;
}

.form-group input {
  width: 100%;
  padding: 1rem 1.25rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1.1rem;
  background-color: #ffffff;
  transition: all 0.2s ease;
}

.form-group input:focus {
  outline: none;
  border-color: #0071e3;
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.1);
}

.error-message {
  color: #ff3b30;
  font-size: 0.875rem;
  margin-top: 0.5rem;
}

.login-button {
  background-color: #0071e3;
  color: white;
  border: none;
  padding: 1rem;
  margin-top: 1rem;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.login-button:hover {
  background-color: #0077ed;
  transform: translateY(-1px);
}

.login-button:active {
  transform: translateY(0);
}

.register-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #86868b;
}

.register-link a {
  color: #0071e3;
  text-decoration: none;
  cursor: pointer;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>